package com.example.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.example.entity.Account;
import com.example.entity.Comment;
import com.example.mapper.CommentMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Resource
    private CommentMapper commentMapper;

    public void add(Comment comment) {
        Account currentUser = TokenUtils.getCurrentUser();
        comment.setUserId(currentUser.getId());
        comment.setTime(DateUtil.now());
        commentMapper.insert(comment);

        Integer id = comment.getId(); //插入后拿到返回的主键id
        if(comment.getPid() == null){    //pid为空说明它不是一条回复评论，是一条父级评论
            comment.setRootId(id);       //因此它的rootID就是它自己的id
        }else {
            Integer parentId = comment.getPid();
            Comment pComment = commentMapper.selectById(parentId);//如果有pid，就通过pid查到父级评论，因为pid就是父级评论的id
            comment.setRootId(pComment.getRootId());  //将父级的rootId插入到该子评论的rootID
        }
        commentMapper.updateById(comment); //更新数据库
    }

    public PageInfo<Comment> selectPage(Comment comment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> list=commentMapper.selectAll(comment);
        return PageInfo.of(list);
    }
    public Comment selectById(Integer id) {
        return commentMapper.selectById(id);
    }
    public List<Comment> selectAll(Comment comment) {
        List<Comment> list=commentMapper.selectAll(comment);
        return list;
    }

    public void updateById(Comment comment) {
        commentMapper.updateById(comment);
    }

    public void deleteById(Integer id) {
        commentMapper.deleteById(id);
    }

    public void deepDelete(Integer pid) {
        commentMapper.deleteById(pid);
        List<Comment> comments = commentMapper.selectByPid(pid);
        if(CollUtil.isNotEmpty(comments)){
            for(Comment comment : comments){
                this.deepDelete(comment.getId());
            }
        }
    }

    public void deleteBatch(Integer[] ids) {
        for (Integer id : ids) {
            commentMapper.deleteById(id);
        }
    }

    public PageInfo<Comment> selectTree(String module, String fid ,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> rootList = commentMapper.selectByModuleAndFid(module, fid);
        for (Comment comment : rootList) {
            Integer rootId = comment.getRootId();
            List<Comment> children = commentMapper.selectByRootId(rootId);
            comment.setChildren(children);
        }
        return PageInfo.of(rootList);
    }

    public Integer selectCount(String module, Integer fid) {
        return commentMapper.selectCount(module,fid);
    }
}
